package com.zzqsecurity.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzqsecurity.entity.TRole;
import com.zzqsecurity.entity.TUser;
import com.zzqsecurity.entity.TUserRole;
import com.zzqsecurity.entity.vo.UserVO;
import com.zzqsecurity.util.HandleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.zzqsecurity.entity.ResultEnum.C9001;

/**
 * @author iszengziqiang@163.com
 * @date 2021-08-25 15:42
 * @Version: 1.0
 * @desc
 */
@Slf4j
@Service
public class UserService implements UserDetailsService {
    @Autowired
    TUserService tUserService;

    @Autowired
    TUserRoleService tUserRoleService;

    @Autowired
    TRoleService tRoleService;


    /**
     * Locates the user based on the username. In the actual implementation, the search
     * may possibly be case sensitive, or case insensitive depending on how the
     * implementation instance is configured. In this case, the <code>UserDetails</code>
     * object that comes back may have a username that is of a different case than what
     * was actually requested..
     *
     * @param username the username identifying the user whose data is required.
     * @return a fully populated user record (never <code>null</code>)
     * @throws UsernameNotFoundException if the user could not be found or the user has no
     *                                   GrantedAuthority
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("获取的用户名为：" + username);
        TUser one = tUserService.getOne(new QueryWrapper<TUser>()
                        .eq("username", username)
                , false);
        if (one == null) {
            throw new HandleException(C9001);
        }
        System.out.println("用户信息是：" + JSON.toJSONString(one));
        UserVO vo = new UserVO();
        //获取权限
        List<TUserRole> userRoleList = tUserRoleService.list(new QueryWrapper<TUserRole>()
                .eq("user_id", one.getId())
        );
        if (userRoleList != null && userRoleList.size() > 0) {
            List<Integer> roleId = userRoleList.stream().map(TUserRole::getRoleId).collect(Collectors.toList());
            List<TRole> roleList = tRoleService.list(new QueryWrapper<TRole>()
                    .in("id", roleId)
            );
            vo.setRoleList(roleList);
        }
        BeanUtils.copyProperties(one, vo);
        System.out.println(JSON.toJSONString(vo)+"转换后的用户");
        return vo;
    }

    public static void main(String[] args) {
        System.out.println(new BCryptPasswordEncoder().encode("123456"));
    }
}
